package com.hardydou.common.controller;

import com.hardydou.system.model.LoginUserDetails;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ：hardy
 * @description：通用控制器
 * @date ：Created in 2020-03-04
 * @Copyright ：Hardydou.com
 */
public class BaseController {


    /**
     * 获取 session
     *
     * @return
     */
    protected static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取 HttpServletRequest
     *
     * @return
     */
    protected static HttpServletRequest getRequest() {
        return getServletRequestAttributes().getRequest();
    }

    /**
     * 获取  HttpServletResponse
     *
     * @return
     */
    protected static HttpServletResponse getResponse() {
        return getServletRequestAttributes().getResponse();
    }

    protected static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    protected LoginUserDetails getLoginUserDetails() {
        return (LoginUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
